{extend name="../../common/view/template/admin" /}

{block name='style'}
<style>
    .login-page {
        margin-top: 100px;
    }
</style>
{/block}

{block name='body'}
<div class="login-page">
    <div class="layui-row">
        <div class="layui-col-md4 layui-col-md-offset4">
            <div class="layui-card">
                <div class="layui-card-header">后台登录</div>
                <div class="layui-card-body">
                    <form class="layui-form" lay-filter="form-box">
                        <div class="padding-top padding-right-xxxl">
                            <div class="layui-form-item">
                                <label class="layui-form-label">帐号：</label>
                                <div class="layui-input-block">
                                    <input type="text" name="username" required lay-verify="required" placeholder="请输入帐号" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">密码：</label>
                                <div class="layui-input-block">
                                    <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" class="layui-input">
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item text-center margin-top-xxl">
                            <button class="layui-btn" lay-submit lay-filter="login">&emsp;登录&emsp;</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}

{block name="script"}
<script>
    layui.use(['element', 'layer', 'form'], function () {
        var $ = layui.jquery;
        var element = layui.element;
        var layer = layui.layer;
        var form = layui.form;

        // 登录
        form.on("submit(login)", function (obj) {
            layer.load(1);
            $.ajax({
                url: "{:url('apiadmin/index/login')}",
                method: 'post',
                headers: {
                    'authorization': getCookie('admin_auth')
                },
                data: obj.field,
                dataType: 'JSON',
                success: function (res) {
                    layer.closeAll();
                    if (res.errcode === 0) {
                        setCookie("admin_auth", res.data.auth);

                        layer.msg('登录成功', {icon: 1, time: 1000}, function () {
                            window.location.href = "{:url('main/index')}";
                        });
                    } else {
                        layer.alert(res.errmsg, {icon: 2});
                    }
                },
                error: function (res) {
                    layer.closeAll();
                    layer.alert("网络错误");
                }
            });
            return false;
        });
    });
</script>
{/block}